﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using MinistryStandardPlatformEntity;
using MinistryStandardPlatformWeb.Models.ReportCenter;

namespace MinistryStandardPlatformWeb.Controllers.Reports
{
    [MenuAuthorized("42")]
    public class InstructionHistoryController : Controller
    {
        private MSPDataContext context = new MSPDataContext();
        //
        // GET: /InstructionHistory/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetData(string startTime, string endTime, List<string> sims)
        {
            if (!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime) && sims != null && sims.Count != 0)
            {
                startTime = startTime.Replace('-','/');
                endTime = endTime.Replace('-', '/');
                List<InstructionHistory> list = (from t in this.context.CmdLogPara
                                                 where sims.Contains(t.CmdSim)
                                                 && t.CmdTime.CompareTo(startTime) >= 0 && t.CmdTime.CompareTo(endTime) <= 0
                                                 select new InstructionHistory()
                                                 {
                                                     mSender = t.UserName,
                                                     mSim = t.CmdSim,
                                                     mInstruction = "指令:" + t.CmdName + " 内容:" + t.CmdContent,
                                                     mSendTime = t.CmdTime
                                                 }).ToList();
                var json = new
                {
                    total = list.Count,
                    rows = list
                };
                var serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = Int32.MaxValue;
                return Content(serializer.Serialize(json), "application/json");
            }
            return null;
        }

        [HttpPost]
        [MenuAuthorized("1", Order = 1)]
        public ActionResult ExportExcel()
        {
            List<InstructionHistory> list = CommonFunctions.JsonStrToObject<List<InstructionHistory>>(Request.Form["data"]);
            var sbHtml = new System.Text.StringBuilder();
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "序号", "指令发送方", "所属单位", "车牌号", "SIM卡号", "下发指令", "下发时间" };
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");

            for (int i = 0; i < list.Count; i++)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td>{0}</td>", i + 1);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mSender);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mDepart);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mVehicleName);
                sbHtml.AppendFormat("<td style='vnd.ms-excel.numberformat:@;'>{0}</td>", list[i].mSim);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mInstruction);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mSendTime);
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");

            //第一种:使用FileContentResult
            byte[] fileContents = System.Text.Encoding.Default.GetBytes(sbHtml.ToString());
            return File(fileContents, "application/ms-excel", "指令下发历史.xls");

            ////第二种:使用FileStreamResult
            //var fileStream = new MemoryStream(fileContents);
            //return File(fileStream, "application/ms-excel", "fileStream.xls");

            ////第三种:使用FilePathResult
            ////服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回.
            //var fileName = Server.MapPath("~/Files/fileName.xls");
            //return File(fileName, "application/ms-excel", "fileName.xls");
            //return null;
        }
    }
}
